import { getAllRoleList, isUserExist } from '/@/api/demo/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { Switch } from 'ant-design-vue';
import { setUserStatus } from '/@/api/demo/system';
import { useMessage } from '/@/hooks/web/useMessage';
import { h } from 'vue';
export const columns: BasicColumn[] = [
  {
    title: '姓名',
    dataIndex: 'name',
    width: 120,
  },
  {
    title: '用户名',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '所属角色',
    dataIndex: 'roleName',
    width: 120,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          setUserStatus(record.userId, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '登录失败次数',
    dataIndex: 'locked',
    width: 120,
  },
  {
    title: '创建时间',
    dataIndex: 'gmtCreated',
  },
  {
    title: '登录ip',
    dataIndex: 'lastLoginIp',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '用户名称',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    defaultValue: '1',
    componentProps: {
      options: [
        {
          label: '启用',
          value: '1',
          key: '1',
        },
        {
          label: '禁用',
          value: '0',
          key: '0',
        },
      ],
    },
    colProps: {
      span: 4,
    },
  },
  {
    field: 'locked',
    label: '是否被锁',
    component: 'Select',
    defaultValue: 'false',
    componentProps: {
      options: [
        {
          label: '账号未锁定',
          value: 'false',
          key: 'false',
        },
        {
          label: '账号锁定',
          value: 'true',
          key: 'true',
        },
      ],
    },
    colProps: {
      span: 6,
    },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    helpMessage: ['不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
      {
        validator(_, value) {
          return new Promise((resolve, reject) => {
            isUserExist(value)
              .then((res) => {
                if (res != null) {
                  throw { code: 0, msg: '存在用户名' };
                }
                resolve()
              })
              .catch((err) => {
                reject(err.message || '验证失败,存在相同用户名');
              });
          });
        },
      },
    ],
    ifShow: ({ values }) => {
      return values.userId==null||values.userId=='';
    },
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
  },
  {
    label: '角色',
    field: 'roleId',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'name',
      valueField: 'roleId',
    },
    required: true,
  },
  {
    field: 'name',
    label: '昵称',
    component: 'Input',
    required: true,
  },

  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: true,
  },

  {
    label: '备注',
    field: 'description',
    component: 'InputTextArea',
  },
  {
    label: '登录ip',
    field: 'lastLoginIp',
    component: 'Input',
    required: false,
    ifShow: false
  },
  {
    label: '用户id',
    field: 'userId',
    component: 'Input',
    required: true,
    ifShow: true
  },
];
